/**
 * 
 */
package sailing.ui.systemsetting;

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTree;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.SubjectCategory;
import sailing.listener.DATA;
import sailing.listener.UI;
import sailing.managers.AccountingSubjectManager;
import sailing.resource.Resource;
import sailing.ui.AbstractJPanel;
import sailing.ui.base.TopTitlePanel;
import sailing.ui.systemsetting.model.SubjectTreeModel;
import sailing.ui.tabbedPane.MyWindowsTabbedPaneUI;
import cjlite.logger.Logger;
import cjlite.utils.Lists;

import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class AccountingSubjectMgtPanel extends AbstractJPanel {
	private static Logger logger = Logger.thisClass();

	private static ImageIcon add = Resource.getImageIcon("/ui/tree/add.png");
	private static ImageIcon minus = Resource.getImageIcon("/ui/tree/minus.png");

	private JTree tree;
	private JButton addNewBtn;
	private SubjectTreeModel stm;


	private JTabbedPane tabPanel;

	private List<SubjectTablePanel> stpList;

	/**
	 * @param application
	 */
	public AccountingSubjectMgtPanel(SingleFrameApplication application) {
		super(application);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.AbstractJPanel#initialPanel()
	 */
	@Override
	protected void initialPanel() {
		this.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 0));
		this.setLayout(new BorderLayout(10, 10));
		//this.add(this.getTopTitlePanel(), BorderLayout.NORTH);
		this.add(new TopTitlePanel("会计科目管理"), BorderLayout.NORTH);
		
		
		
		tabPanel = new JTabbedPane();
		MyWindowsTabbedPaneUI mwtpui = new MyWindowsTabbedPaneUI();
		tabPanel.setUI(mwtpui);

		this.addSubjectCategoryPanel(tabPanel);

		this.add(tabPanel, BorderLayout.CENTER);

		this.add(this.getSidebarPanel(), BorderLayout.EAST);

	}

	/**
	 * @param tabPanel2
	 */
	private void addSubjectCategoryPanel(JTabbedPane tabPanel2) {
		AccountingSubjectManager asm = this.getApplication().getContext().getIoc()
				.getInstance(AccountingSubjectManager.class);

		List<SubjectCategory> scList = asm.getSubjectCategoryList();
		if (stpList == null) {
			stpList = Lists.newArrayList();
		}
		stpList.clear();
		for (int i = 0; i < scList.size(); i++) {
			SubjectTablePanel stp = new SubjectTablePanel(scList.get(i), this.getApplication());
			stpList.add(stp);
			DATA.addDataListener(stp);
			tabPanel.addTab(stp.getTabName(), stp.getTabIcon(), stp.getContentPanel());
		}
	}

	ActionListener editAl = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			logger.entry();
		}

	};

	ActionListener deleteAl = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			logger.entry();
		}

	};

	private JPanel getTopTitlePanel() {
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEtchedBorder());
		panel.setBackground(titleBgColor);
		panel.add(new JLabel("会计科目管理"));
		return panel;
	}

	private JPanel getSidebarPanel() {
		JPanel panel = new JPanel();
		addNewBtn = new JButton();
		addNewBtn.setText("添加科目");
		addNewBtn.setMargin(new Insets(5, 30, 5, 30));
		addNewBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				NewAccountingSubjectDialog nasd = new NewAccountingSubjectDialog(getApplication().getMainFrame(),
						getApplication());

				if (stpList != null) {
					int index = tabPanel.getSelectedIndex();
					SubjectTablePanel stp = stpList.get(index);
					nasd.setPreSubjectCategory(stp.getSubjectCategory());
				}

				nasd.setVisible(true);
			}
		});

		JCheckBox showhideEditBtn = new JCheckBox();
		showhideEditBtn.setText("显示隐藏编辑按钮");

		showhideEditBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JCheckBox cb = (JCheckBox) e.getSource();
				logger.trace("checkbox is selected[{0}]", cb.isSelected());
				showHideEditBtn(cb.isSelected());
			}
		});

		JButton refreshBtn = new JButton();
		refreshBtn.setText("刷新数据");
		refreshBtn.setMargin(new Insets(5, 30, 5, 30));
		refreshBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				UI.refresh(SubjectTablePanel.class);
			}
		});

		JPanel pane = new JPanel();
		FormLayout layout = new FormLayout("f:pref", "pref,8dlu,pref,8dlu,pref");

		pane.setLayout(layout);

		pane.add(refreshBtn, CC.xy(1, 1));

		pane.add(addNewBtn, CC.xy(1, 3));

		pane.add(showhideEditBtn, CC.xy(1, 5));
		//
		// Box bv = Box.createVerticalBox();
		// bv.add(refreshBtn);
		// bv.add(Box.createVerticalStrut(20));
		// bv.add(addNewBtn);
		// bv.add(Box.createVerticalStrut(20));
		// bv.add(showhideEditBtn);
		panel.add(pane);
		return panel;
	}

	protected void showHideEditBtn(boolean selected) {
		if (this.stpList == null) {
			return;
		}

		for (SubjectTablePanel stp : stpList) {
			stp.showHideEditBtn(selected);
		}
	}

}
